package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.exception.LoginException;
import cn.wolfcode.wms.mapper.EmployeeMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.EmployeeQueryObject;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public void deleteById(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Employee employee, Long[] ids) {
        employeeMapper.deleteRelation(employee.getId());
        if (employee.getId() != null) {
            employeeMapper.updateByPrimaryKey(employee);
        } else {
            employeeMapper.insert(employee);
        }
        if (ids != null) {

            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(), rid);
            }
        }
    }

    public Employee getById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    public PageResult query(EmployeeQueryObject qo) {
        Integer totalCount = employeeMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Employee> data = employeeMapper.queryForList(qo);
        return new PageResult(totalCount,data, qo.getCurrentPage(), qo.getPageSize());
    }

    @Override
    public void login(Employee employee) {
        Employee empindb = employeeMapper.selectLogin(employee.getName());
        if (empindb == null) {
            throw new LoginException("用户名或密码错误");
        }
        if (!empindb.getPassword().equals(employee.getPassword())) {
            throw new LoginException("用户名或密码错误");
        }
        //登陆成功,将数据添加到session中
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
        attributes.getRequest().getSession().setAttribute("USER_IN_SESIION", empindb);

    }

    @Override
    public void deleteAll(Long[] ids) {
        if (ids != null) {
            System.out.println("123111111111111111111111112222");
            for (Long id : ids) {
                employeeMapper.deleteRelation(id);
                employeeMapper.deleteByPrimaryKey(id);
            }
        }
    }

}
